package com.qf.config.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author xufuhan
 * @Date 2024/9/11 17:54
 */

@Component
@Data
@ConfigurationProperties(prefix = "jwt")
public class JwtUtil {
    private String issuer;
    private String secret;
    private int expiration;

    //生成token
    public String generateToken(Map<String ,String> map){
        //获取当前时间
        Calendar instance= Calendar.getInstance();
        //设置过期时间
        instance.add(Calendar.MINUTE,expiration);

        //获取jwt
        JWTCreator.Builder builder = JWT.create();
        //设置自定义参数
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        //生成token
        //设置颁发者，颁发时间，过期时间
        String token = builder.withIssuer(issuer).withIssuedAt(new Date())
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(secret));//设置秘钥
        return token;
    }

    //验证token
    public boolean check(String token){
        try {
            JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        }catch (JWTVerificationException e){
            return false;
        }
        return true;
    }

    //token解析
    public DecodedJWT jwtDecode(String token){
        try {
            return JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        }catch (SignatureVerificationException e){
            throw  new RuntimeException("token签名错误");
        }catch (AlgorithmMismatchException e){
            throw new RuntimeException("token算法不匹配");
        }catch (TokenExpiredException e){
            throw new RuntimeException("token已过期");
        }catch (Exception e){
            throw new RuntimeException("token解析失败");
        }
    }
}
